/*
 * 文件名：BpastCaeCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.bpast.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.bpast.service.BpastCaeCollectService;
import top.infopub.job.bpast.service.CaeCollectService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpast.CaeCollect;
import top.infopub.model.hr.OrgInfo;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;

@Service
public class BpastCaeCollectServiceImpl implements BpastCaeCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private CaeCollectService caeCollectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bpastCaeCollectJob() {
        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            CaeCollect caeCollect = new CaeCollect();
            //往表里存放项目信息
            caeCollect.setProCode(proContract.getProCode());
            caeCollect.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                caeCollect.setOrgProName(orgInfos.get(0).getCompName());
                caeCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    caeCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    caeCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            caeCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            caeCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            caeCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //合同建安费(元)
            BigDecimal caeFee = proContractService.selectCaeFeeByProContractId(proContract.getId());
            caeCollect.setCotCaeAmount(caeFee);
            //当月产值(下浮前)对外(元)
            Map<String, Object> outSureWhereBefore = Maps.newHashMap();
            outSureWhereBefore.put("sureMonth", caeCollect.getStsMonth());
            outSureWhereBefore.put("proCode", caeCollect.getProCode());
            BigDecimal proQaOfOutSureBofore = outcaeSureService.selectProQaOfOutSureBefore(outSureWhereBefore);
            caeCollect.setMonthOutPre(proQaOfOutSureBofore);
            //当月产值(下浮后)对外(元)
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", caeCollect.getStsMonth());
            outSureWhere.put("proCode", caeCollect.getProCode());
            BigDecimal proQaOfOutSure = outcaeSureService.selectProQaOfOutSure(outSureWhere);
            caeCollect.setMonthOutAfter(proQaOfOutSure);
            //当月产值(下浮前)对内(元)
            Map<String, Object> innerSureWhereBefore = Maps.newHashMap();
            innerSureWhereBefore.put("sureMonth", caeCollect.getStsMonth());
            innerSureWhereBefore.put("proCode", caeCollect.getProCode());
            BigDecimal proQaOfInnerSureBofore = incaeSureService.selectProQaOfInnerSureBefore(outSureWhereBefore);
            caeCollect.setMonthInPre(proQaOfInnerSureBofore);
            //当月产值(下浮后)对内(元)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", caeCollect.getStsMonth());
            innerSureWhere.put("proCode", caeCollect.getProCode());
            BigDecimal proQaOfInnerSure = incaeSureService.selectProQaOfInnerSure(innerSureWhere);
            caeCollect.setMonthInAfter(proQaOfInnerSure);
            //建安毛利(元)
            Map<String, Object> grossProfitWhere = Maps.newHashMap();
            grossProfitWhere.put("sureMonth", caeCollect.getStsMonth());
            grossProfitWhere.put("proCode", caeCollect.getProCode());
            BigDecimal grossProfit = outcaeSureService.selectProQaOfGrossProfit(grossProfitWhere);
            caeCollect.setMonthCaeGross(grossProfit);
            //判断主表中是否已经存在开累的数据
            Map<String, Object> where = Maps.newHashMap();
            where.put("proCode", proContract.getProCode());
            where.put("dataFlag", 1);
            where.put("orderCondition", "STS_MONTH desc");
            List<CaeCollect> caeCollects = caeCollectService.selectByMapWhere(where);
            if (caeCollects != null && caeCollects.size() > 0) {
                CaeCollect collect = caeCollects.get(0);
                //开累产值-下浮前(对内(元))ADDUP_IN_PRE
                caeCollect.setAddupInPre(proQaOfInnerSureBofore.add(collect.getAddupInPre()));
                //开累产值-下浮前(对外(元))ADDUP_OUT_PRE
                caeCollect.setAddupOutPre(proQaOfOutSureBofore.add(collect.getAddupOutPre()));
                //开累产值-下浮后(对内(元))ADDUP_IN_AFTER
                caeCollect.setAddupInAfter(proQaOfInnerSure.add(collect.getAddupInAfter()));
                //开累产值-下浮后(对外(元))ADDUP_OUT_AFTER
                caeCollect.setAddupOutAfter(proQaOfOutSure.add(collect.getAddupOutAfter()));
                //开累建安毛利(元)ADDUP_CAE_GROSS
                caeCollect.setAddupCaeGross(grossProfit.add(collect.getAddupCaeGross()));
            }
            else {
                //开累产值-下浮前(对内(元))
                caeCollect.setAddupInPre(proQaOfInnerSureBofore);
                //开累产值-下浮前(对外(元))
                caeCollect.setAddupOutPre(proQaOfOutSureBofore);
                //开累产值-下浮后(对内(元))
                caeCollect.setAddupInAfter(proQaOfInnerSure);
                //开累产值-下浮后(对外(元))
                caeCollect.setAddupOutAfter(proQaOfOutSure);
                //开累建安毛利(元)
                caeCollect.setAddupCaeGross(grossProfit);
            }
            //开累完工比(产值)(%)  开累产值-下浮前(对外(元))/合同建安费(元)
            if (caeCollect.getCotCaeAmount() != null
                && caeCollect.getCotCaeAmount() != BigDecimal.ZERO) {
                BigDecimal addupFinishRate = caeCollect.getAddupOutPre().divide(
                    caeCollect.getCotCaeAmount(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED);
                caeCollect.setAddupFinishRate(addupFinishRate);
            }
            else {
                caeCollect.setAddupFinishRate(BigDecimal.ZERO);
            }
            //数据状态(默认为有效)
            caeCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            caeCollect.setAddBy("system");
            caeCollect.setAddTime(new Date());
            caeCollect.setUptBy("system");
            caeCollect.setUptTime(new Date());
            caeCollectService.insert(caeCollect);
        }
    }
}